﻿#region License
/*
 
Quick Survey for WSS 3.0 and MOSS 2007
Copyright (C) 2008 Darren Johnstone (http://darrenjohnstone.net)
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;

namespace RLSoft.SharePoint.FAQ
{
    /// <summary>
    /// ToolPart allowing selection of a list on the current site via a drop down list.
    /// </summary>
    public class GenericListSelectorToolPart : ToolPart
    {
        private string _ListFieldName;
        private Control _parentUserControl;
        private Guid _ListFeatureId = Guid.Empty;
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericListSelectorToolPart"/> class.
        /// </summary>
        private GenericListSelectorToolPart()
            : base()
        {
            _ListFieldName = "ListName";
        }

        public GenericListSelectorToolPart(Control parentUserControl)
            : this(parentUserControl, "ListName")
        {
        }

        public GenericListSelectorToolPart(Control parentUserControl, string strListFieldName)
            : this(parentUserControl, strListFieldName, Guid.Empty)
        {
        }

        public GenericListSelectorToolPart(Control parentUserControl, string strListFieldName, Guid listFeatureId)
            : base()
        {
            _ListFieldName = strListFieldName;
            _parentUserControl = parentUserControl;
            _ListFeatureId = listFeatureId;
        }

        public GenericListSelectorToolPart(string strListFieldName)
            : base()
        {
            _ListFieldName = strListFieldName;
        }

        /// <summary>
        /// Sends the tool part content to the specified HtmlTextWriter object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="output">The HtmlTextWriter object that receives the tool part content.</param>
        protected override void RenderToolPart(HtmlTextWriter output)
        {
            //Type webPartType = typeof(UserControl);
            string strListName = string.Empty;
            try
            {

                PropertyInfo info = _parentUserControl.GetType().GetProperty(_ListFieldName);
                strListName = (string)info.GetValue(_parentUserControl, null);
                //strListName = _parentType.InvokeMember(_ListFieldName,    BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,    null, _parentUserControl, null) as string;
            }
            catch (Exception ex)
            {

                output.Write(string.Format("Your Web Part doesn't contain a {0} property. {1}", _ListFieldName, ex.ToString()));
                return;
            }



            output.Write("<table cellspacing=\"0\" border=\"0\" style=\"border-width:0px;width:100%;border-collapse:collapse;\">");
            output.Write("<tr><td>");
            //output.Write("<div class=\"UserSectionHead\">");
            //output.Write(Properties.Resources.picker_list);
            //output.Write("</div>");
            output.Write("<div class=\"UserControlGroup\">");
            output.Write("<select name=\"picker_list\" style=\"width:100%\" id=\"picker_list\">");

            foreach (SPList l in SPContext.Current.Web.Lists)
            {
                bool displayList = false;
                if (!l.Hidden)
                {
                    if (_ListFeatureId != Guid.Empty)
                    {
                        if (l.TemplateFeatureId == _ListFeatureId)
                        {
                            displayList = true;
                        }
                    }
                    else
                    {
                        displayList = true;
                    }
                    if (displayList)
                    {
                        output.Write("<option value=\"" + l.Title + "\"");
                        if (l.Title == strListName) output.Write(" selected=\"true\"");
                        output.Write(">");
                        output.Write(l.Title);
                        output.Write("</option>");
                    }

                }
            }

            output.Write("</select>");
            output.Write("</div>");
            output.Write("</td></tr>");
            output.Write("</table>");

        }

        /// <summary>
        /// Called when the user clicks the OK or the Apply button in the tool pane.
        /// </summary>
        public override void ApplyChanges()
        {

            try
            {
                PropertyInfo info = _parentUserControl.GetType().GetProperty(_ListFieldName);
                info.SetValue(_parentUserControl, Page.Request.Form["picker_list"], null);
                //SmartPart.SmartPart selectedWebPart = (SmartPart.SmartPart)this.ParentToolPane.SelectedWebPart;
                //selectedWebPart.SetCustomProperty(_ListFieldName, Page.Request.Form["picker_list"]);
                //selectedWebPart.RefreshPropertyValues();
                //selectedWebPart.SaveWebPartProperties = true;
            }
            catch (Exception ex)
            {
                string strError = ex.ToString();
                //output.Write(string.Format("Could not save to ListName. Error: {0}", ex));
            }
        }

        //public override void SyncChanges()
        //{
        //    ApplyChanges();
        //    ApplyChanges();
        //    base.SyncChanges();
        //}
    }
}
